package com.whsxt.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConfigServerController {
    @Autowired
    private RestTemplate restTemplate;
    //端口不写死,从配置文件中获取
    @Value("${server.port}")
    private Integer port;
    //因为git发送请求到我们这里，我们发送一个请求到 config-server里面
    @PostMapping("/myRefresh")
    public ResponseEntity<String> myRefresh(){
        String url="http://localhost"+port+"/actuator/bus-refresh";
        //设置一个请求头
        HttpHeaders httpHeaders=new HttpHeaders();
        httpHeaders.add("content-type","application/json;charset=utf-8");
        HttpEntity<String> httpEntity=new HttpEntity<>(httpHeaders);
        //响应体
        ResponseEntity<String> responseEntity=restTemplate.postForEntity(url,httpEntity,String.class);
        //判断请求的响应体
        if(responseEntity.getStatusCode().equals(HttpStatus.NO_CONTENT)){
            //成功了，给git返回一个成功
            return ResponseEntity.ok("刷新配置文件成功");
        }
        return ResponseEntity.badRequest().body("刷新失败" );
    }
}
